using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.IO;
using WinCommand.SnippetManager;
using System.Diagnostics;
using AppModule.InterProcessComm;
using WinCommand.CommandPipe;
using System.Text;


namespace WinCommand
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    internal class WinCmdGUI : System.Windows.Forms.Form
    {

        #region Constants and Enumerations

        #endregion

        #region Members Variables

        private System.ComponentModel.IContainer components;

        private System.Windows.Forms.Panel pnlControls;
        private System.Windows.Forms.Splitter splitter1;
        private System.Windows.Forms.StatusBar sBar;
        private System.Windows.Forms.TabPage tbpStatus;
        private System.Windows.Forms.TextBox txtStatus;
        private System.Windows.Forms.Splitter splitter2;
        private System.Windows.Forms.TextBox txtErrors;
        private System.Windows.Forms.TabControl tabControl;
        private System.Windows.Forms.TabPage tbpDebug;
        private System.Windows.Forms.TabControl tabs;
        private System.Windows.Forms.SaveFileDialog saveFileDialog;
        private System.Windows.Forms.ToolTip imgCloseTip;
        private System.Windows.Forms.PictureBox imgClose;
        private System.Windows.Forms.Panel pnlConsoleControls;
        private System.Windows.Forms.PictureBox imgSaveHistory;
        private System.Windows.Forms.PictureBox imgSnippet;
        private ShellTextBox shWinCmd;
        private MenuStrip mnuStripMain;
        private ToolStripMenuItem fileToolStripMenuItem;
        private ToolStripMenuItem saveConsoleTextToolStripMenuItem;
        private ToolStripMenuItem alwaysToolStripMenuItem;
        private ToolStripSeparator toolStripSeparator1;
        private ToolStripMenuItem exitToolStripMenuItem;
        private ToolStripMenuItem historyToolStripMenuItem;
        private ToolStripMenuItem saveCommandsToolStripMenuItem;
        private ToolStripMenuItem clearToolStripMenuItem;
        private ToolStripMenuItem toolsToolStripMenuItem;
        private ToolStripMenuItem globalCommandsToolStripMenuItem;
        private ToolStripMenuItem helpToolStripMenuItem;
        private ToolStripSeparator toolStripSeparator2;
        private ToolStripMenuItem abouitToolStripMenuItem;
        private ToolStripMenuItem toolStripMenuItem2;
        private ToolStripMenuItem EditToolStripMenuItem;
        private ToolStripMenuItem mnuEditUndo;
        private ToolStripMenuItem mnuEditRedo;
        private ToolStripSeparator toolStripSeparator11;
        private ToolStripMenuItem mnuEditCut;
        private ToolStripMenuItem mnuEditCopy;
        private ToolStripMenuItem mnuEditPaste;
        private ToolStripSeparator toolStripSeparator12;
        private ToolStripMenuItem mnuEditSelectAll;
        private ToolStripMenuItem selectBetweenToolStripMenuItem;

        private IniFile _iniFile;
        private string helpText = string.Empty;
        private const string APP_NAME = "WinCommand";
        private ArrayList _commandActiveCtrls;
        private ToolStripSeparator toolStripSeparator3;
        private ToolStripMenuItem editWinCommandiniToolStripMenuItem;
        private GlobalLoader _gl;
        private ToolStripMenuItem clearStatusToolStripMenuItem;

        private SnippetUI _snippetWnd;
        private IChannelManager _PipeManager;
        private bool _isActive = false;
        #endregion

        #region Events ans Delegates Declaration

        public delegate void StatusUpdatedCallBack(String text);
        public delegate void GuiUpdateCallBack(bool push);
        public delegate void BringToFrontCallBack();

        #endregion

        private void LoadGlobalLoader() {
            _gl = new GlobalLoader();
            _gl.Dock = DockStyle.Fill;

            _gl.WinCmdToExecute = shWinCmd;
            tabs.SelectedTab.Controls.Add(_gl);
            tabs.Enabled = true;
        }

        #region Constructor/Destructor

        public WinCmdGUI()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            _iniFile = new IniFile();
            _iniFile.Load();
            Location = _iniFile.Location;
            SetTopMost(_iniFile.AlwaysOnTop);

            tabs.TabPages.Add(new TabPage("WinCommand"));
            foreach (AppConf conf in _iniFile.Applications.Values)
            {
                tabs.TabPages.Add(new TabPage(conf.Name));
            }

            shWinCmd.Dock = System.Windows.Forms.DockStyle.Fill;

            shWinCmd.StatusUpdated += new ShellTextBox.StatusUpdatedEventHandler(shWinCmd_StatusUpdated);
            shWinCmd.DebuggerUpdated += new ShellTextBox.DebuggerUpdatedEventHandler(shWinCmd_DebuggerUpdated);

            shWinCmd.CmdExitExecuted += new ShellTextBox.CmdExitAppEventHandler(shWinCmd_CmdExitExecuted);
            shWinCmd.CmdKillExecuted += new ShellTextBox.CmdKillAppEventHandler(shWinCmd_CmdKillExecuted);

            _commandActiveCtrls = new ArrayList();
            _commandActiveCtrls.Add(saveConsoleTextToolStripMenuItem);
            
            _commandActiveCtrls.Add(mnuEditUndo);
            _commandActiveCtrls.Add(mnuEditRedo);
            _commandActiveCtrls.Add(mnuEditCopy);
            _commandActiveCtrls.Add(mnuEditCut);
            _commandActiveCtrls.Add(mnuEditPaste);
            _commandActiveCtrls.Add(mnuEditSelectAll);

            _commandActiveCtrls.Add(saveCommandsToolStripMenuItem);
            _commandActiveCtrls.Add(clearToolStripMenuItem);

            _commandActiveCtrls.Add(globalCommandsToolStripMenuItem);
            _commandActiveCtrls.Add(selectBetweenToolStripMenuItem);
            _commandActiveCtrls.Add(globalCommandsToolStripMenuItem);
            _commandActiveCtrls.Add(selectBetweenToolStripMenuItem);
            _commandActiveCtrls.Add(exitToolStripMenuItem);

            _PipeManager = new PipeManager();
            ((PipeManager)_PipeManager).PipeEvent += new PipeManager.PipeEventHandler(WinCmdGUI_PipeEvent);
            _PipeManager.Initialize();

            ChangeCmdActiveCtrlState(false);
        }

        void WinCmdGUI_PipeEvent(object sender, PipeEventArgs e)
        {
            if (!_isActive) return;

            if (e.pEvent == PipeEventArgs.PipeEvent.Message)
            {
                shWinCmd.WriteText(new StringBuilder(e.Message + Environment.NewLine), true);
                shWinCmd.CWrapper.WriteLineToStdInput(e.Message);

                EndPipeCallBack();
            }
        }

        private void EndOfPipe() {
            this.Size = new Size(630, 485);
            this.Activate();
        }

        private void EndPipeCallBack()
        {
            if (InvokeRequired)
                this.Invoke(new BringToFrontCallBack(EndOfPipe));
            else
                EndOfPipe();
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
                shWinCmd.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(WinCmdGUI));
            this.pnlControls = new System.Windows.Forms.Panel();
            this.tabs = new System.Windows.Forms.TabControl();
            this.splitter1 = new System.Windows.Forms.Splitter();
            this.sBar = new System.Windows.Forms.StatusBar();
            this.tabControl = new System.Windows.Forms.TabControl();
            this.tbpStatus = new System.Windows.Forms.TabPage();
            this.txtStatus = new System.Windows.Forms.TextBox();
            this.tbpDebug = new System.Windows.Forms.TabPage();
            this.txtErrors = new System.Windows.Forms.TextBox();
            this.splitter2 = new System.Windows.Forms.Splitter();
            this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
            this.imgCloseTip = new System.Windows.Forms.ToolTip(this.components);
            this.pnlConsoleControls = new System.Windows.Forms.Panel();
            this.imgSnippet = new System.Windows.Forms.PictureBox();
            this.imgSaveHistory = new System.Windows.Forms.PictureBox();
            this.imgClose = new System.Windows.Forms.PictureBox();
            this.mnuStripMain = new System.Windows.Forms.MenuStrip();
            this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.saveConsoleTextToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.alwaysToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.editWinCommandiniToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
            this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.clearStatusToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.EditToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.mnuEditUndo = new System.Windows.Forms.ToolStripMenuItem();
            this.mnuEditRedo = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator11 = new System.Windows.Forms.ToolStripSeparator();
            this.mnuEditCut = new System.Windows.Forms.ToolStripMenuItem();
            this.mnuEditCopy = new System.Windows.Forms.ToolStripMenuItem();
            this.mnuEditPaste = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator12 = new System.Windows.Forms.ToolStripSeparator();
            this.mnuEditSelectAll = new System.Windows.Forms.ToolStripMenuItem();
            this.historyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.saveCommandsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.clearToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.globalCommandsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.selectBetweenToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.helpToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
            this.abouitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.shWinCmd = new WinCommand.ShellTextBox();
            this.pnlControls.SuspendLayout();
            this.tabControl.SuspendLayout();
            this.tbpStatus.SuspendLayout();
            this.tbpDebug.SuspendLayout();
            this.pnlConsoleControls.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.imgSnippet)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.imgSaveHistory)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.imgClose)).BeginInit();
            this.mnuStripMain.SuspendLayout();
            this.SuspendLayout();
            // 
            // pnlControls
            // 
            this.pnlControls.BackColor = System.Drawing.SystemColors.Control;
            this.pnlControls.Controls.Add(this.tabs);
            this.pnlControls.Dock = System.Windows.Forms.DockStyle.Top;
            this.pnlControls.Location = new System.Drawing.Point(0, 24);
            this.pnlControls.Name = "pnlControls";
            this.pnlControls.Size = new System.Drawing.Size(622, 24);
            this.pnlControls.TabIndex = 1;
            // 
            // tabs
            // 
            this.tabs.Appearance = System.Windows.Forms.TabAppearance.FlatButtons;
            this.tabs.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tabs.ImeMode = System.Windows.Forms.ImeMode.Off;
            this.tabs.Location = new System.Drawing.Point(0, 0);
            this.tabs.Name = "tabs";
            this.tabs.SelectedIndex = 0;
            this.tabs.Size = new System.Drawing.Size(622, 24);
            this.tabs.TabIndex = 23;
            this.tabs.Selecting += new System.Windows.Forms.TabControlCancelEventHandler(this.tabs_Selecting);
            this.tabs.SelectedIndexChanged += new System.EventHandler(this.tabs_SelectedIndexChanged);
            // 
            // splitter1
            // 
            this.splitter1.BackColor = System.Drawing.SystemColors.Control;
            this.splitter1.Dock = System.Windows.Forms.DockStyle.Top;
            this.splitter1.Location = new System.Drawing.Point(0, 48);
            this.splitter1.Name = "splitter1";
            this.splitter1.Size = new System.Drawing.Size(622, 8);
            this.splitter1.TabIndex = 2;
            this.splitter1.TabStop = false;
            // 
            // sBar
            // 
            this.sBar.Location = new System.Drawing.Point(0, 436);
            this.sBar.Name = "sBar";
            this.sBar.Size = new System.Drawing.Size(622, 22);
            this.sBar.TabIndex = 13;
            // 
            // tabControl
            // 
            this.tabControl.Appearance = System.Windows.Forms.TabAppearance.FlatButtons;
            this.tabControl.Controls.Add(this.tbpStatus);
            this.tabControl.Controls.Add(this.tbpDebug);
            this.tabControl.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.tabControl.HotTrack = true;
            this.tabControl.ItemSize = new System.Drawing.Size(42, 18);
            this.tabControl.Location = new System.Drawing.Point(0, 343);
            this.tabControl.Name = "tabControl";
            this.tabControl.Padding = new System.Drawing.Point(0, 0);
            this.tabControl.SelectedIndex = 0;
            this.tabControl.Size = new System.Drawing.Size(622, 93);
            this.tabControl.TabIndex = 16;
            // 
            // tbpStatus
            // 
            this.tbpStatus.Controls.Add(this.txtStatus);
            this.tbpStatus.Location = new System.Drawing.Point(4, 22);
            this.tbpStatus.Name = "tbpStatus";
            this.tbpStatus.Size = new System.Drawing.Size(614, 67);
            this.tbpStatus.TabIndex = 0;
            this.tbpStatus.Text = "Status";
            // 
            // txtStatus
            // 
            this.txtStatus.BackColor = System.Drawing.Color.Black;
            this.txtStatus.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtStatus.Dock = System.Windows.Forms.DockStyle.Fill;
            this.txtStatus.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
            this.txtStatus.Location = new System.Drawing.Point(0, 0);
            this.txtStatus.Multiline = true;
            this.txtStatus.Name = "txtStatus";
            this.txtStatus.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.txtStatus.Size = new System.Drawing.Size(614, 67);
            this.txtStatus.TabIndex = 0;
            // 
            // tbpDebug
            // 
            this.tbpDebug.BackColor = System.Drawing.Color.Black;
            this.tbpDebug.Controls.Add(this.txtErrors);
            this.tbpDebug.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
            this.tbpDebug.Location = new System.Drawing.Point(4, 22);
            this.tbpDebug.Name = "tbpDebug";
            this.tbpDebug.Size = new System.Drawing.Size(608, 67);
            this.tbpDebug.TabIndex = 1;
            this.tbpDebug.Text = "Debug";
            this.tbpDebug.Visible = false;
            // 
            // txtErrors
            // 
            this.txtErrors.BackColor = System.Drawing.Color.Black;
            this.txtErrors.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtErrors.Dock = System.Windows.Forms.DockStyle.Fill;
            this.txtErrors.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
            this.txtErrors.Location = new System.Drawing.Point(0, 0);
            this.txtErrors.Multiline = true;
            this.txtErrors.Name = "txtErrors";
            this.txtErrors.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.txtErrors.Size = new System.Drawing.Size(608, 67);
            this.txtErrors.TabIndex = 1;
            // 
            // splitter2
            // 
            this.splitter2.BackColor = System.Drawing.SystemColors.Control;
            this.splitter2.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.splitter2.Location = new System.Drawing.Point(0, 340);
            this.splitter2.Name = "splitter2";
            this.splitter2.Size = new System.Drawing.Size(622, 3);
            this.splitter2.TabIndex = 17;
            this.splitter2.TabStop = false;
            // 
            // pnlConsoleControls
            // 
            this.pnlConsoleControls.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.pnlConsoleControls.Controls.Add(this.imgSnippet);
            this.pnlConsoleControls.Controls.Add(this.imgSaveHistory);
            this.pnlConsoleControls.Controls.Add(this.imgClose);
            this.pnlConsoleControls.Dock = System.Windows.Forms.DockStyle.Top;
            this.pnlConsoleControls.Location = new System.Drawing.Point(0, 56);
            this.pnlConsoleControls.Name = "pnlConsoleControls";
            this.pnlConsoleControls.Size = new System.Drawing.Size(622, 20);
            this.pnlConsoleControls.TabIndex = 21;
            this.pnlConsoleControls.Visible = false;
            // 
            // imgSnippet
            // 
            this.imgSnippet.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.imgSnippet.Cursor = System.Windows.Forms.Cursors.Hand;
            this.imgSnippet.Image = ((System.Drawing.Image)(resources.GetObject("imgSnippet.Image")));
            this.imgSnippet.Location = new System.Drawing.Point(570, 0);
            this.imgSnippet.Name = "imgSnippet";
            this.imgSnippet.Size = new System.Drawing.Size(14, 14);
            this.imgSnippet.TabIndex = 23;
            this.imgSnippet.TabStop = false;
            this.imgSnippet.Click += new System.EventHandler(this.imgSnippet_Click);
            // 
            // imgSaveHistory
            // 
            this.imgSaveHistory.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.imgSaveHistory.Cursor = System.Windows.Forms.Cursors.Hand;
            this.imgSaveHistory.Image = ((System.Drawing.Image)(resources.GetObject("imgSaveHistory.Image")));
            this.imgSaveHistory.Location = new System.Drawing.Point(586, 0);
            this.imgSaveHistory.Name = "imgSaveHistory";
            this.imgSaveHistory.Size = new System.Drawing.Size(14, 14);
            this.imgSaveHistory.TabIndex = 22;
            this.imgSaveHistory.TabStop = false;
            this.imgSaveHistory.Click += new System.EventHandler(this.imgSaveHistory_Click);
            // 
            // imgClose
            // 
            this.imgClose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.imgClose.Cursor = System.Windows.Forms.Cursors.Hand;
            this.imgClose.Image = ((System.Drawing.Image)(resources.GetObject("imgClose.Image")));
            this.imgClose.Location = new System.Drawing.Point(603, 0);
            this.imgClose.Name = "imgClose";
            this.imgClose.Size = new System.Drawing.Size(14, 14);
            this.imgClose.TabIndex = 21;
            this.imgClose.TabStop = false;
            this.imgClose.Click += new System.EventHandler(this.imgClose_Click);
            // 
            // mnuStripMain
            // 
            this.mnuStripMain.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fileToolStripMenuItem,
            this.EditToolStripMenuItem,
            this.historyToolStripMenuItem,
            this.toolsToolStripMenuItem,
            this.helpToolStripMenuItem});
            this.mnuStripMain.Location = new System.Drawing.Point(0, 0);
            this.mnuStripMain.Name = "mnuStripMain";
            this.mnuStripMain.Size = new System.Drawing.Size(622, 24);
            this.mnuStripMain.TabIndex = 24;
            this.mnuStripMain.Text = "menuStrip1";
            // 
            // fileToolStripMenuItem
            // 
            this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.saveConsoleTextToolStripMenuItem,
            this.alwaysToolStripMenuItem,
            this.toolStripSeparator1,
            this.editWinCommandiniToolStripMenuItem,
            this.toolStripSeparator3,
            this.exitToolStripMenuItem,
            this.clearStatusToolStripMenuItem});
            this.fileToolStripMenuItem.ForeColor = System.Drawing.Color.Black;
            this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
            this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
            this.fileToolStripMenuItem.Text = "&File";
            // 
            // saveConsoleTextToolStripMenuItem
            // 
            this.saveConsoleTextToolStripMenuItem.Name = "saveConsoleTextToolStripMenuItem";
            this.saveConsoleTextToolStripMenuItem.Size = new System.Drawing.Size(192, 22);
            this.saveConsoleTextToolStripMenuItem.Text = "Save Console Text";
            this.saveConsoleTextToolStripMenuItem.Click += new System.EventHandler(this.saveConsoleTextToolStripMenuItem_Click);
            // 
            // alwaysToolStripMenuItem
            // 
            this.alwaysToolStripMenuItem.Name = "alwaysToolStripMenuItem";
            this.alwaysToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Alt | System.Windows.Forms.Keys.A)));
            this.alwaysToolStripMenuItem.Size = new System.Drawing.Size(192, 22);
            this.alwaysToolStripMenuItem.Text = "Always On Top";
            this.alwaysToolStripMenuItem.Click += new System.EventHandler(this.alwaysToolStripMenuItem_Click);
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new System.Drawing.Size(189, 6);
            // 
            // editWinCommandiniToolStripMenuItem
            // 
            this.editWinCommandiniToolStripMenuItem.Name = "editWinCommandiniToolStripMenuItem";
            this.editWinCommandiniToolStripMenuItem.Size = new System.Drawing.Size(192, 22);
            this.editWinCommandiniToolStripMenuItem.Text = "Edit WinCommand.ini";
            this.editWinCommandiniToolStripMenuItem.Click += new System.EventHandler(this.editWinCommandiniToolStripMenuItem_Click);
            // 
            // toolStripSeparator3
            // 
            this.toolStripSeparator3.Name = "toolStripSeparator3";
            this.toolStripSeparator3.Size = new System.Drawing.Size(189, 6);
            // 
            // exitToolStripMenuItem
            // 
            this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
            this.exitToolStripMenuItem.Size = new System.Drawing.Size(192, 22);
            this.exitToolStripMenuItem.Text = "Kill Console";
            this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
            // 
            // clearStatusToolStripMenuItem
            // 
            this.clearStatusToolStripMenuItem.Name = "clearStatusToolStripMenuItem";
            this.clearStatusToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Shift | System.Windows.Forms.Keys.F5)));
            this.clearStatusToolStripMenuItem.Size = new System.Drawing.Size(192, 22);
            this.clearStatusToolStripMenuItem.Text = "Clear status";
            this.clearStatusToolStripMenuItem.Click += new System.EventHandler(this.clearStatusToolStripMenuItem_Click);
            // 
            // EditToolStripMenuItem
            // 
            this.EditToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.mnuEditUndo,
            this.mnuEditRedo,
            this.toolStripSeparator11,
            this.mnuEditCut,
            this.mnuEditCopy,
            this.mnuEditPaste,
            this.toolStripSeparator12,
            this.mnuEditSelectAll});
            this.EditToolStripMenuItem.ForeColor = System.Drawing.Color.Black;
            this.EditToolStripMenuItem.Name = "EditToolStripMenuItem";
            this.EditToolStripMenuItem.Size = new System.Drawing.Size(39, 20);
            this.EditToolStripMenuItem.Text = "&Edit";
            // 
            // mnuEditUndo
            // 
            this.mnuEditUndo.Image = ((System.Drawing.Image)(resources.GetObject("mnuEditUndo.Image")));
            this.mnuEditUndo.ImageTransparentColor = System.Drawing.Color.Black;
            this.mnuEditUndo.Name = "mnuEditUndo";
            this.mnuEditUndo.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Z)));
            this.mnuEditUndo.Size = new System.Drawing.Size(164, 22);
            this.mnuEditUndo.Text = "&Undo";
            this.mnuEditUndo.Click += new System.EventHandler(this.mnuEditUndo_Click);
            // 
            // mnuEditRedo
            // 
            this.mnuEditRedo.Image = ((System.Drawing.Image)(resources.GetObject("mnuEditRedo.Image")));
            this.mnuEditRedo.ImageTransparentColor = System.Drawing.Color.Black;
            this.mnuEditRedo.Name = "mnuEditRedo";
            this.mnuEditRedo.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Y)));
            this.mnuEditRedo.Size = new System.Drawing.Size(164, 22);
            this.mnuEditRedo.Text = "&Redo";
            this.mnuEditRedo.Click += new System.EventHandler(this.mnuEditRedo_Click);
            // 
            // toolStripSeparator11
            // 
            this.toolStripSeparator11.Name = "toolStripSeparator11";
            this.toolStripSeparator11.Size = new System.Drawing.Size(161, 6);
            // 
            // mnuEditCut
            // 
            this.mnuEditCut.Image = ((System.Drawing.Image)(resources.GetObject("mnuEditCut.Image")));
            this.mnuEditCut.ImageTransparentColor = System.Drawing.Color.Black;
            this.mnuEditCut.Name = "mnuEditCut";
            this.mnuEditCut.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.X)));
            this.mnuEditCut.Size = new System.Drawing.Size(164, 22);
            this.mnuEditCut.Text = "Cu&t";
            this.mnuEditCut.Click += new System.EventHandler(this.mnuEditCut_Click);
            // 
            // mnuEditCopy
            // 
            this.mnuEditCopy.Image = ((System.Drawing.Image)(resources.GetObject("mnuEditCopy.Image")));
            this.mnuEditCopy.ImageTransparentColor = System.Drawing.Color.Black;
            this.mnuEditCopy.Name = "mnuEditCopy";
            this.mnuEditCopy.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.C)));
            this.mnuEditCopy.Size = new System.Drawing.Size(164, 22);
            this.mnuEditCopy.Text = "&Copy";
            this.mnuEditCopy.Click += new System.EventHandler(this.mnuEditCopy_Click);
            // 
            // mnuEditPaste
            // 
            this.mnuEditPaste.Image = ((System.Drawing.Image)(resources.GetObject("mnuEditPaste.Image")));
            this.mnuEditPaste.ImageTransparentColor = System.Drawing.Color.Black;
            this.mnuEditPaste.Name = "mnuEditPaste";
            this.mnuEditPaste.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.V)));
            this.mnuEditPaste.Size = new System.Drawing.Size(164, 22);
            this.mnuEditPaste.Text = "&Paste";
            this.mnuEditPaste.Click += new System.EventHandler(this.mnuEditPaste_Click);
            // 
            // toolStripSeparator12
            // 
            this.toolStripSeparator12.Name = "toolStripSeparator12";
            this.toolStripSeparator12.Size = new System.Drawing.Size(161, 6);
            // 
            // mnuEditSelectAll
            // 
            this.mnuEditSelectAll.Name = "mnuEditSelectAll";
            this.mnuEditSelectAll.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.A)));
            this.mnuEditSelectAll.Size = new System.Drawing.Size(164, 22);
            this.mnuEditSelectAll.Text = "Select &All";
            this.mnuEditSelectAll.Click += new System.EventHandler(this.mnuEditSelectAll_Click);
            // 
            // historyToolStripMenuItem
            // 
            this.historyToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.saveCommandsToolStripMenuItem,
            this.clearToolStripMenuItem});
            this.historyToolStripMenuItem.ForeColor = System.Drawing.Color.Black;
            this.historyToolStripMenuItem.Name = "historyToolStripMenuItem";
            this.historyToolStripMenuItem.Size = new System.Drawing.Size(57, 20);
            this.historyToolStripMenuItem.Text = "&History";
            // 
            // saveCommandsToolStripMenuItem
            // 
            this.saveCommandsToolStripMenuItem.Name = "saveCommandsToolStripMenuItem";
            this.saveCommandsToolStripMenuItem.Size = new System.Drawing.Size(163, 22);
            this.saveCommandsToolStripMenuItem.Text = "Save Commands";
            this.saveCommandsToolStripMenuItem.Click += new System.EventHandler(this.saveCommandsToolStripMenuItem_Click);
            // 
            // clearToolStripMenuItem
            // 
            this.clearToolStripMenuItem.Name = "clearToolStripMenuItem";
            this.clearToolStripMenuItem.Size = new System.Drawing.Size(163, 22);
            this.clearToolStripMenuItem.Text = "Clear";
            this.clearToolStripMenuItem.Click += new System.EventHandler(this.clearToolStripMenuItem_Click);
            // 
            // toolsToolStripMenuItem
            // 
            this.toolsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.globalCommandsToolStripMenuItem,
            this.selectBetweenToolStripMenuItem});
            this.toolsToolStripMenuItem.ForeColor = System.Drawing.Color.Black;
            this.toolsToolStripMenuItem.Name = "toolsToolStripMenuItem";
            this.toolsToolStripMenuItem.Size = new System.Drawing.Size(48, 20);
            this.toolsToolStripMenuItem.Text = "&Tools";
            // 
            // globalCommandsToolStripMenuItem
            // 
            this.globalCommandsToolStripMenuItem.Name = "globalCommandsToolStripMenuItem";
            this.globalCommandsToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.G)));
            this.globalCommandsToolStripMenuItem.Size = new System.Drawing.Size(205, 22);
            this.globalCommandsToolStripMenuItem.Text = "Exec Global Cmd";
            this.globalCommandsToolStripMenuItem.Click += new System.EventHandler(this.globalCommandsToolStripMenuItem_Click);
            // 
            // selectBetweenToolStripMenuItem
            // 
            this.selectBetweenToolStripMenuItem.Name = "selectBetweenToolStripMenuItem";
            this.selectBetweenToolStripMenuItem.Size = new System.Drawing.Size(205, 22);
            this.selectBetweenToolStripMenuItem.Text = "Select Between";
            this.selectBetweenToolStripMenuItem.Click += new System.EventHandler(this.selectBetweenToolStripMenuItem_Click);
            // 
            // helpToolStripMenuItem
            // 
            this.helpToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripMenuItem2,
            this.toolStripSeparator2,
            this.abouitToolStripMenuItem});
            this.helpToolStripMenuItem.ForeColor = System.Drawing.Color.Black;
            this.helpToolStripMenuItem.Name = "helpToolStripMenuItem";
            this.helpToolStripMenuItem.Size = new System.Drawing.Size(44, 20);
            this.helpToolStripMenuItem.Text = "&Help";
            // 
            // toolStripMenuItem2
            // 
            this.toolStripMenuItem2.Image = ((System.Drawing.Image)(resources.GetObject("toolStripMenuItem2.Image")));
            this.toolStripMenuItem2.ImageTransparentColor = System.Drawing.Color.Black;
            this.toolStripMenuItem2.Name = "toolStripMenuItem2";
            this.toolStripMenuItem2.Size = new System.Drawing.Size(136, 22);
            this.toolStripMenuItem2.Text = "&Commands";
            // 
            // toolStripSeparator2
            // 
            this.toolStripSeparator2.Name = "toolStripSeparator2";
            this.toolStripSeparator2.Size = new System.Drawing.Size(133, 6);
            // 
            // abouitToolStripMenuItem
            // 
            this.abouitToolStripMenuItem.Name = "abouitToolStripMenuItem";
            this.abouitToolStripMenuItem.Size = new System.Drawing.Size(136, 22);
            this.abouitToolStripMenuItem.Text = "About ...";
            this.abouitToolStripMenuItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click);
            // 
            // shWinCmd
            // 
            this.shWinCmd.AcceptsTab = true;
            this.shWinCmd.AllowDrop = true;
            this.shWinCmd.BackColor = System.Drawing.Color.Black;
            this.shWinCmd.CWrapper = null;
            this.shWinCmd.Dock = System.Windows.Forms.DockStyle.Fill;
            this.shWinCmd.Font = new System.Drawing.Font("Lucida Console", 8.25F);
            this.shWinCmd.ForeColor = System.Drawing.Color.LawnGreen;
            this.shWinCmd.IsClosing = false;
            this.shWinCmd.Location = new System.Drawing.Point(0, 76);
            this.shWinCmd.MaxLength = 0;
            this.shWinCmd.Name = "shWinCmd";
            this.shWinCmd.Size = new System.Drawing.Size(622, 264);
            this.shWinCmd.TabIndex = 23;
            this.shWinCmd.Text = "";
            this.shWinCmd.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.shWinCmd_LinkClicked);
            // 
            // WinCmdGUI
            // 
            this.AllowDrop = true;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.BackColor = System.Drawing.Color.Black;
            this.ClientSize = new System.Drawing.Size(622, 458);
            this.Controls.Add(this.shWinCmd);
            this.Controls.Add(this.pnlConsoleControls);
            this.Controls.Add(this.splitter2);
            this.Controls.Add(this.tabControl);
            this.Controls.Add(this.sBar);
            this.Controls.Add(this.splitter1);
            this.Controls.Add(this.pnlControls);
            this.Controls.Add(this.mnuStripMain);
            this.ForeColor = System.Drawing.SystemColors.Control;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MainMenuStrip = this.mnuStripMain;
            this.Name = "WinCmdGUI";
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.Text = "WinCommand";
            this.Deactivate += new System.EventHandler(this.WinCmdGUI_Deactivate);
            this.Activated += new System.EventHandler(this.WinCmdGUI_Activated);
            this.Closing += new System.ComponentModel.CancelEventHandler(this.WinCmdGUI_Closing);
            this.pnlControls.ResumeLayout(false);
            this.tabControl.ResumeLayout(false);
            this.tbpStatus.ResumeLayout(false);
            this.tbpStatus.PerformLayout();
            this.tbpDebug.ResumeLayout(false);
            this.tbpDebug.PerformLayout();
            this.pnlConsoleControls.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.imgSnippet)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.imgSaveHistory)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.imgClose)).EndInit();
            this.mnuStripMain.ResumeLayout(false);
            this.mnuStripMain.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

        #endregion

        #region Properties

        
        #endregion

        #region Public Methods

        #endregion

        #region Protected Methods

        #endregion

        #region Private Methods

        private void shWinCmd_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            ProcessStartInfo pInfo = new ProcessStartInfo(e.LinkText);
            Process.Start(pInfo);
        }

        private void WinCmdGUI_Activated(object sender, EventArgs e)
        {
            if (!TopMost) return;

            Opacity = 1.0;
        }

        private void WinCmdGUI_Deactivate(object sender, EventArgs e)
        {
            if (!TopMost) return;

            Opacity = 0.4;
        }

        private void ChangeCmdActiveCtrlState(bool state) {
            _isActive = state;

            foreach (ToolStripMenuItem item in _commandActiveCtrls) {
                item.Enabled = state;
            }
        }

        private void StartCmdGuiUpdate(bool push)
        {
            if (push)
            {
                tabs.Enabled = false;
                tabs.Font = new Font(tabs.Font.Name, tabs.Font.Size, FontStyle.Italic);
                pnlConsoleControls.Visible = true;
                Text += " - " + tabs.SelectedTab.Text;
            }
            else
            {
                tabs.Enabled = true;
                tabs.Font = new Font(tabs.Font.Name, tabs.Font.Size, FontStyle.Regular);
                tabs.SelectedIndex = 0;
                pnlConsoleControls.Visible = false;
                Text = "WinCommand";
            }
            Application.DoEvents();
        }

        private void UpdateDebugger(string msg)
        {
            txtErrors.Text += "[" + DateTime.Now.ToString("HH:MM:ss") + "] " + msg + Environment.NewLine;
            txtErrors.SelectionLength = 0;
            txtErrors.SelectionStart = txtErrors.Text.Length;
            txtErrors.ScrollToCaret();
        }

        private void UpdateStatus(string msg)
        {

            txtStatus.Text = txtStatus.Text.Insert(0, msg);
            txtStatus.SelectionLength = 0;
            txtStatus.SelectionStart = 0;
            txtStatus.ScrollToCaret();
        }

        //Save initialization file
        private bool ICmd_SaveIni()
        {
            _iniFile.Location = Location;
            _iniFile.Save();
            return true;
        }

        //Start a console inside WinCommand
        private void ICmd_Start(string command)
        {
            if (InvokeRequired)
                this.Invoke(new GuiUpdateCallBack(StartCmdGuiUpdate), new object[] { true });
            else
                StartCmdGuiUpdate(true);

            shWinCmd.CmdHistory.ClearHistory();

            string appToStartName = command.Replace(_iniFile.SysCmdPrefix + ShellTextBox.CMD_START + ShellTextBox.SPACE, string.Empty);
            shWinCmd.StartConsoleApp(appToStartName, _iniFile);
        }

        private bool ICmd_Save_History()
        {
            saveFileDialog.ShowDialog();

            string selectedPath = saveFileDialog.FileName;
            if (!selectedPath.Equals(string.Empty))
            {
                shWinCmd.CmdHistory.SaveHistoryToFile(selectedPath);
                return true;
            }
            else
                return false;
        }

        private bool ProcessGUICommand(string command)
        {
            if (command.StartsWith(_iniFile.SysCmdPrefix + ShellTextBox.CMD_SAVE_CONFIG))
            {
                return ICmd_SaveIni();
            }
            else if (command.StartsWith(_iniFile.SysCmdPrefix + ShellTextBox.CMD_SAVE_HISTORY))
            {
                return ICmd_Save_History();
            }
            else
                return false;
        }

        private void SetTopMost(bool state) {
            TopMost = state;
            alwaysToolStripMenuItem.Checked = state;
            _iniFile.AlwaysOnTop = state;
        }

        #endregion

        #region Events Handlers

        #region Edit

        private void mnuEditUndo_Click(object sender, EventArgs e)
        {
            shWinCmd.Undo();
        }

        private void mnuEditRedo_Click(object sender, EventArgs e)
        {
            shWinCmd.Redo();
        }

        private void mnuEditSelectAll_Click(object sender, EventArgs e)
        {
            shWinCmd.SelectAll();
        }

        private void mnuEditCopy_Click(object sender, EventArgs e)
        {
            try
            {
                if (ActiveControl.GetType().Equals(shWinCmd.GetType()))
                    shWinCmd.Copy();
                else
                    ((TextBoxBase)ActiveControl).Copy();
            }
            catch { }
        }

        private void mnuEditPaste_Click(object sender, EventArgs e)
        {
            try
            {
                if (ActiveControl.GetType().Equals(shWinCmd.GetType()))
                    shWinCmd.Paste();
                else
                    ((TextBoxBase)ActiveControl).Paste();
            }
            catch { }
        }

        private void mnuEditCut_Click(object sender, EventArgs e)
        {
            try
            {
                if (ActiveControl.GetType().Equals(shWinCmd.GetType()))
                    shWinCmd.Cut();
                else
                    ((TextBoxBase)ActiveControl).Cut();
            }
            catch { }
        }
        #endregion

        private void selectBetweenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            shWinCmd.SelectBetweenPairs();
        }

        private void imgSaveHistory_Click(object sender, System.EventArgs e)
        {
            ProcessGUICommand(_iniFile.SysCmdPrefix + ShellTextBox.CMD_SAVE_HISTORY);
        }

        private void imgSnippet_Click(object sender, System.EventArgs e)
        {
            if (_snippetWnd != null) {
                _snippetWnd.Close();
                _snippetWnd.Dispose();
            }

            _snippetWnd = new SnippetUI();
            _snippetWnd.MdiParent = this.MdiParent;

            _snippetWnd.Width = GetLPanelWidth();
            _snippetWnd.Height = GetLPanelHeight();
            _snippetWnd.Location = new Point(GetLPanelXPos(), GetLPanelYPos());
            _snippetWnd.Show();
            _snippetWnd.Focus();
        }

        private void shWinCmd_DebuggerUpdated(object sender, DebuggerUpdatedEventArgs e)
        {
            string text = "[" + DateTime.Now.ToString("HH:MM:ss") + "] " + e.Message + Environment.NewLine;
            if (InvokeRequired)
                this.Invoke(new StatusUpdatedCallBack(UpdateDebugger), new object[] { text });
            else
                UpdateStatus(text);
        }

        [System.Runtime.InteropServices.DllImport("KERNEL32.DLL")]
        public static extern bool SetEnvironmentVariable(string VariableName, string VariableValue);

        private void tabs_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (tabs.SelectedIndex != 0)
            {
                try
                {
                    _iniFile = new IniFile();
                    _iniFile.Load();

                    ICmd_Start(_iniFile.SysCmdPrefix + ShellTextBox.CMD_START + " " + tabs.SelectedTab.Text);
                    imgCloseTip.SetToolTip(imgClose, "Close " + shWinCmd.CWrapper.ConsoleId + " Console");
                    imgCloseTip.SetToolTip(imgSaveHistory, "Save history...");
                    imgCloseTip.SetToolTip(imgSnippet, "Snippet Manager...");
                    ChangeCmdActiveCtrlState(true);

                    LoadGlobalLoader();
                }
                catch(Exception ex)
                {
                    UpdateStatus(ex.Message);
                    imgCloseTip.RemoveAll();
                    ChangeCmdActiveCtrlState(false);
                }
            }
        }

        private void imgClose_Click(object sender, System.EventArgs e)
        {
            shWinCmd.EndExecutingApp();
            txtStatus.Clear();

            ChangeCmdActiveCtrlState(false);
        }

        private void WinCmdGUI_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            exitToolStripMenuItem_Click(sender, e);
        }

        void shWinCmd_CmdKillExecuted(object sender, CommandEnteredEventArgs e)
        {
            ICmd_SaveIni();
            Application.Exit();
        }

        void shWinCmd_CmdExitExecuted(object sender, CommandEnteredEventArgs e)
        {
            if (InvokeRequired)
                this.Invoke(new GuiUpdateCallBack(StartCmdGuiUpdate), new object[] { false });
            else
                StartCmdGuiUpdate(false);
        }

        private void shWinCmd_StatusUpdated(object sender, StatusUpdatedEventArgs e)
        {
            string text = "[" + DateTime.Now.ToString("HH:MM:ss") + "] " + e.Message + Environment.NewLine;
            if (InvokeRequired)
                this.Invoke(new StatusUpdatedCallBack(UpdateStatus), new object[] { text });
            else
                UpdateStatus(text);
        }

        private void saveConsoleTextToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog.ShowDialog();
            string selectedPath = saveFileDialog.FileName;

            StreamWriter sw = new StreamWriter(selectedPath, false);
            sw.WriteLine(shWinCmd.Text.Replace("\n", "\r\n"));
            sw.Close();
        }

        private void alwaysToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SetTopMost(!TopMost);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ProcessGUICommand(_iniFile.SysCmdPrefix + ShellTextBox.CMD_SAVE_CONFIG);
            shWinCmd.KillConsole();
        }

        private void saveCommandsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ProcessGUICommand(_iniFile.SysCmdPrefix + ShellTextBox.CMD_SAVE_HISTORY);
        }

        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            shWinCmd.CmdHistory.ClearHistory();
        }

        private void globalCommandsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_gl != null)
                _gl.ExecGlobalLoader();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SetTopMost(false);
            About a = new About();
            a.TopMost = true;
            a.StartPosition = FormStartPosition.CenterParent;
            a.ShowDialog();
            SetTopMost(true);
        }

        #endregion

        #region Helpers

        private int GetLPanelXPos()
        {
            return Location.X + shWinCmd.Location.X + shWinCmd.Width - GetLPanelWidth() - 14;
        }

        private int GetLPanelYPos()
        {
            return Location.Y + shWinCmd.Location.Y + pnlConsoleControls.Height + 10;
        }

        private int GetLPanelWidth()
        {
            return shWinCmd.Width / 3;
        }

        private int GetLPanelHeight()
        {
            return shWinCmd.Height - 5;
        }

        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        public static void Main(string[] args)
        {
            try
            {
                WinCmdGUI app = new WinCmdGUI();
                //Form1 app = new Form1();
                Application.Run(app);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message); 
            }
            finally
            {
                
            }
        }

        private void tabs_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if (e.TabPageIndex != 0 && shWinCmd.CWrapper != null && !shWinCmd.CWrapper.HasExited)
                e.Cancel = true;
        }

        private void editWinCommandiniToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (shWinCmd != null && shWinCmd.CWrapper != null && shWinCmd.CWrapper.IsStarted)
            {
                shWinCmd.EndExecutingApp();
                txtStatus.Clear();
            }

            EditWinCommandIni frmEd = new EditWinCommandIni();
            frmEd.ShowDialog();

            _iniFile = new IniFile();
            _iniFile.Load();
        }

        private void clearStatusToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtStatus.Clear();
        }
    }
}